Option Strict On

Imports System.Drawing
Imports System.Windows.Forms

Namespace xpComponent.Form
    Public Class xpComboBox
        Inherits System.Windows.Forms.ComboBox
        Implements xpComponent.Form.IDisabled

        Private mblnEnabled As Boolean = True
        Private mclrBackColorDisabled As System.Drawing.Color = Color.Gainsboro
        Private mclrForeColorDisabled As System.Drawing.Color = SystemColors.ControlText
        Private mmnuEmpty As System.Windows.Forms.ContextMenu = New System.Windows.Forms.ContextMenu

        Public Property BackColorDisabled() As System.Drawing.Color Implements xpComponent.Form.IDisabled.BackColorDisabled
            Get
                Return mclrBackColorDisabled
            End Get
            Set(ByVal Value As System.Drawing.Color)
                mclrBackColorDisabled = Value
            End Set
        End Property

        Public Property ForeColorDisabled() As System.Drawing.Color Implements xpComponent.Form.IDisabled.ForeColorDisabled
            Get
                Return mclrForeColorDisabled
            End Get
            Set(ByVal Value As System.Drawing.Color)
                mclrForeColorDisabled = Value
            End Set
        End Property

        Public Shadows Property Enabled() As Boolean
            Get
                Return mblnEnabled
            End Get
            Set(ByVal Value As Boolean)
                'Keeps the current Back/Fore color
                Static sclrBackColor As System.Drawing.Color
                Static sclrForeColor As System.Drawing.Color
                Static sobjComboBoxStyle As System.Windows.Forms.ComboBoxStyle
                Static sblnTabStop As Boolean

                If sclrBackColor.IsEmpty Then
                    sclrBackColor = Me.BackColor
                    sclrForeColor = Me.ForeColor
                    sobjComboBoxStyle = Me.DropDownStyle
                    sblnTabStop = Me.TabStop
                End If

                'Stop looping in itself
                Static sblnIsProcessing As Boolean
                If sblnIsProcessing Then Exit Property
                sblnIsProcessing = True

                mblnEnabled = Value
                Me.Enabled = True
                If mblnEnabled Then
                    Me.BackColor = sclrBackColor
                    Me.ForeColor = sclrForeColor
                    Me.DropDownStyle = sobjComboBoxStyle
                    Me.ContextMenu = Nothing
                    Me.TabStop = sblnTabStop
                Else
                    Me.BackColor = Me.BackColorDisabled
                    Me.ForeColor = Me.ForeColorDisabled
                    Me.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple
                    Me.Height = 21
                    Me.ContextMenu = mmnuEmpty
                    Me.TabStop = False
                End If
                Me.Invalidate()

                sblnIsProcessing = False
            End Set
        End Property

        Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
            If Not Me.mblnEnabled AndAlso _
                (e.KeyCode = System.Windows.Forms.Keys.Up OrElse _
                 e.KeyCode = System.Windows.Forms.Keys.Down OrElse _
                 e.KeyCode = System.Windows.Forms.Keys.Delete) Then
                e.Handled = True
            Else
                MyBase.OnKeyDown(e)
            End If
        End Sub

        Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
            If Not Me.mblnEnabled Then
                e.Handled = True
            Else
                MyBase.OnKeyPress(e)
            End If
        End Sub
    End Class
End Namespace